package com.sugar.subscribe.course.module.business.dal.mysql.coachcomment;

import java.math.BigDecimal;
import java.util.*;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.sugar.subscribe.course.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.subscribe.course.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.sugar.subscribe.course.module.business.controller.app.coachcomment.vo.AppCoachCommentPageReqVO;
import com.sugar.subscribe.course.module.business.controller.app.coachcomment.vo.AppCoachCommentRespVO;
import com.sugar.subscribe.course.module.business.dal.dataobject.coach.CoachDO;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachcomment.CoachCommentDO;
import com.sugar.subscribe.course.module.business.dal.dataobject.user.BizUserDO;
import org.apache.ibatis.annotations.Mapper;
import com.sugar.subscribe.course.module.business.controller.admin.coachcomment.vo.*;
import org.apache.ibatis.annotations.Select;

/**
 * 评论 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface CoachCommentMapper extends BaseMapperX<CoachCommentDO> {

    default PageResult<CoachCommentRespVO> selectPage(CoachCommentPageReqVO reqVO) {
        return selectJoinPage(reqVO, CoachCommentRespVO.class, new MPJLambdaWrapperX<CoachCommentDO>()
                .selectAll(CoachCommentDO.class)
                .selectAs(BizUserDO::getNickname, CoachCommentRespVO::getNickname)
                .selectAs(CoachDO::getName, CoachCommentRespVO::getCoachName)
                .leftJoin(CoachDO.class, CoachDO::getId, CoachCommentDO::getCoachId)
                .leftJoin(BizUserDO.class, BizUserDO::getId, CoachCommentDO::getUserId)
                .likeIfPresent(BizUserDO::getNickname, reqVO.getNickname())
                .eqIfPresent(CoachCommentDO::getUserId, reqVO.getUserId())
                .eqIfPresent(CoachCommentDO::getCoachId, reqVO.getCoachId())
                .eqIfPresent(CoachCommentDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(CoachCommentDO::getContent, reqVO.getContent())
                .eqIfPresent(CoachCommentDO::getAuditStatus, reqVO.getAuditStatus())
                .betweenIfPresent(CoachCommentDO::getCreateTime, reqVO.getCreateTime())
                .likeIfPresent(CoachDO::getName, reqVO.getCoachName())
                .orderByDesc(CoachCommentDO::getId));
    }

    default PageResult<AppCoachCommentRespVO> appSelectPage(AppCoachCommentPageReqVO pageReqVO){
        return selectJoinPage(pageReqVO, AppCoachCommentRespVO.class, new MPJLambdaWrapperX<CoachCommentDO>()
                .selectAll(CoachCommentDO.class)
                .selectAs(BizUserDO::getNickname, AppCoachCommentRespVO::getNickname)
                .selectAs(BizUserDO::getAvatar, AppCoachCommentRespVO::getAvatar)
                .leftJoin(BizUserDO.class, BizUserDO::getId, CoachCommentDO::getUserId)
                .eqIfPresent(CoachCommentDO::getCoachId, pageReqVO.getCoachId()));
    }

    @Select("select" +
            " avg(bcc.score) " +
            "from business_coach_comment bcc " +
            "where bcc.deleted = 0 and bcc.audit_status = 10 and bcc.coach_id = #{param1}")
    BigDecimal getNowCoachAvgScore(Long coachId);

    @Select("select" +
            " if(bu.avatar is null, '', bu.avatar) " +
            "from " +
            " (select" +
            "   max(bcc.id) max_id, bcc.user_id" +
            "  from business_coach_comment bcc" +
            "  where bcc.deleted = 0 and bcc.audit_status = 10 and bcc.coach_id = #{param1}" +
            "  group by bcc.user_id order by max_id desc limit 3) temp_tbl " +
            "left join business_user bu on temp_tbl.user_id = bu.id and bu.deleted = 0")
    List<String> selectTopThreeUserCommentUserAvatar(Long coachId);
}